package com.czapl.tabs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.czapl.api.IUnit;
import com.czapl.mygdxgame.ConstructableField;
import com.czapl.mygdxgame.MyGdxGame;

public class CUI extends Stage {

	public static final float MENU_WIDTH = 150;
	public static final int TAB_COUNT = 3;
	
	protected Table table;
	protected Table m_menu;
	protected CTab m_currentTab;
	protected Table spacers;
	protected Image topSpacer, bottomSpacer;//, menuBar;
	
	public CUI() {
		//super();
        NinePatch patch = MyGdxGame.skin.get("button_default", NinePatch.class);
        topSpacer = new Image(patch);
        bottomSpacer = new Image(patch);
        m_menu = new Table();
        m_menu.setBackground(new NinePatchDrawable(patch));
	}
	
	void showTab(CTab tab)
	{
		m_menu.removeActor(m_currentTab);
		m_currentTab = tab;
		m_menu.add(m_currentTab).colspan(TAB_COUNT);
	}
	
	public void showInfoTab(ConstructableField field) {
		System.out.println("showing info tab:");
		showTab(new CellInfoTab(field));
	}
	public void showInfoTab(IUnit unit) {
		System.out.println("showing info tab:");
		showTab(new UnitInfoTab(unit));
		
	}
	
	public void resize(float h) {
		System.out.println("resize menu spacer h: "+h);
        spacers.getCell(topSpacer).size(Gdx.graphics.getWidth()-MENU_WIDTH,h);
        spacers.getCell(bottomSpacer).size(Gdx.graphics.getWidth()-MENU_WIDTH,h);
		//a.setWidgetBounds(0, 0, 100, h);
		//topSpacer.setSize(100, h);
	}
	
	protected void addTab(CTab tab, String name) {
		TextButtonStyle buttonStyle = MyGdxGame.skin.get("default", TextButtonStyle.class);
        TextButton tabButton = new TextButton(name, buttonStyle);
        tabButton.addListener(new TabClickListener(tab));
        m_menu.add(tabButton)
    	.size(MENU_WIDTH/3, MENU_WIDTH/3)
    	.top()
    	.left()
    	.expand();
	}
	
	private class TabClickListener extends ClickListener {
		CTab m_tab;
		private TabClickListener(CTab tab) {
			m_tab = tab;
		}
		 @Override
         public void clicked(InputEvent event,float x,float y )
         {
			 System.out.println("tab: "+m_tab);
             showTab(m_tab);
         } 
	}
}
